package org.kumas.bigdata.hdfs3.checkpoint;

import org.kumas.bigdata.hdfs3.fsdirectory.FSDirectory;
import org.kumas.bigdata.hdfs3.rpc.FileSystemProtocolImpl;
import org.kumas.bigdata.hdfs3.serialization.FsSerializations;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import static org.kumas.bigdata.hdfs3.fsdirectory.FSDirectory.SNAP_SHOT;

/*************************************************
 * TODO_Kumas
 *  Author： KumasZhang
 *  DateTime： 2021-12-07 15:11
 *  Description： 
 **/
public class SnapShot implements Runnable {
    private static final FsSerializations SERIALIZATIONS = new FsSerializations();
    @Override
    public void run() {
        try {
            if (!SNAP_SHOT.exists()) {
                SNAP_SHOT.createNewFile();
            }
            synchronized (SNAP_SHOT) {
                try (
                        FileOutputStream fos = new FileOutputStream(SNAP_SHOT);
                        BufferedOutputStream bos = new BufferedOutputStream(fos);
                ) {
                    SERIALIZATIONS.serialize(bos, FileSystemProtocolImpl.FILE_DIR_SYSTEM);
                }
            }
        }catch (IOException e) {
            e.printStackTrace();
        }
    }
}
